﻿#include "formmultigroup.h"
#include "qtpropertymanager.h"
#include "qtvariantproperty.h"
#include "ui_formmultigroup.h"
#include <sstream>

FormMultiGroup::FormMultiGroup(QWidget *parent)
    : QWidget(parent),
      ui(new Ui::FormMultiGroup)
{
    ui->setupUi(this);
    initAll();
}

FormMultiGroup::~FormMultiGroup()
{
    delete ui;
}

void FormMultiGroup::initAll()
{
    m_listPrefix.clear();
    m_listKey.clear();
    m_mapData.clear();

    std::string key;
    std::string prefix;
    std::stringstream ss1;

    for (int i = 0; i < 5; i++)
    {
        ss1.str("");
        ss1 << "group_" << i;
        prefix = ss1.str();

        m_listPrefix << prefix;

        key = prefix + "_x";
        m_mapData[key] = 0;
        m_listKey << key;

        key = prefix + "_y";
        m_mapData[key] = 1;
        m_listKey << key;

        key = prefix + "_a";
        m_mapData[key] = 2;
        m_listKey << key;
    }

    initTreeProp();
}

void FormMultiGroup::initTreeProp()
{
    ui->widget->clear();
    m_listProperty.clear();
    m_listBrowser.clear();
    int total = m_listPrefix.size();

    //标题
    QStringList head;
    head << tr("参数") << tr("数值");
    ui->widget->setHeaderLabels(head);
    ui->widget->setResizeMode(QtTreePropertyBrowser::ResizeToContents);

    //右键菜单
    ui->widget->clearContextMenu();

    QAction *actionCopy = new QAction(tr("复制(C)   Ctrl+C"), ui->widget);
    connect(actionCopy, &QAction::triggered, this, &FormMultiGroup::slotCopy);
    ui->widget->setContextMenu(actionCopy);

    QAction *actionFold = new QAction(tr("折叠"), ui->widget);
    connect(actionFold, &QAction::triggered, this, &FormMultiGroup::slotFold);
    ui->widget->setContextMenu(actionFold);

    QAction *actionUnfold = new QAction(tr("展开"), ui->widget);
    connect(actionUnfold, &QAction::triggered, this, &FormMultiGroup::slotUnfold);
    ui->widget->setContextMenu(actionUnfold);

    //管理
    QtVariantPropertyManager *pVarManager = new QtVariantPropertyManager(ui->widget);
    QtVariantEditorFactory *pVarFactory = new QtVariantEditorFactory(ui->widget);
    ui->widget->setFactoryForManager(pVarManager, pVarFactory);
    connect(pVarManager, &QtVariantPropertyManager::valueChanged, this, &FormMultiGroup::variantPropertyValueChanged);

    //分组数据
    QString groupName;
    for (int i = 0; i < total; i++)
    {
        groupName = QString::fromStdString(m_listPrefix.at(i));
        QtProperty *groupItem = pVarManager->addProperty(QtVariantPropertyManager::groupTypeId(), groupName);
        QtBrowserItem *browserItem = ui->widget->addProperty(groupItem);
        m_listBrowser << browserItem;

        QtVariantProperty *itemX = pVarManager->addProperty(QVariant::Double, tr("X补偿(mm)"));
        itemX->setAttribute(QLatin1String("decimals"), 3);
        itemX->setValue(0);
        groupItem->addSubProperty(itemX);
        m_listProperty << itemX;

        QtVariantProperty *itemY = pVarManager->addProperty(QVariant::Double, tr("Y补偿(mm)"));
        itemY->setAttribute(QLatin1String("decimals"), 3);
        itemY->setValue(0);
        groupItem->addSubProperty(itemY);
        m_listProperty << itemY;

        QtVariantProperty *itemA = pVarManager->addProperty(QVariant::Double, tr("角度补偿(°)"));
        itemA->setAttribute(QLatin1String("decimals"), 3);
        itemA->setValue(0);
        groupItem->addSubProperty(itemA);
        m_listProperty << itemA;
    }
}

void FormMultiGroup::variantPropertyValueChanged(QtProperty *property, const QVariant &value)
{
    QtVariantProperty *p = static_cast<QtVariantProperty *>(property);
    int index = -1;
    for (int i = 0; i < m_listProperty.size(); i++)
    {
        if (m_listProperty[i] == p)
        {
            index = i;
            break;
        }
    }

    if (index != -1)
    {
        m_mapData[m_listKey[index]] = value.toDouble();
    }
}

void FormMultiGroup::slotCopy()
{
    //复制鼠标所在单元格的数据
    QModelIndex index = ui->widget->currentIndex();
    QString str = index.data().toString();
    QApplication::clipboard()->setText(str);
}

void FormMultiGroup::slotFold()
{
    for (int i = 0; i < m_listBrowser.size(); i++)
    {
        ui->widget->setExpanded(m_listBrowser.at(i), false);
    }
}

void FormMultiGroup::slotUnfold()
{
    for (int i = 0; i < m_listBrowser.size(); i++)
    {
        ui->widget->setExpanded(m_listBrowser.at(i), true);
    }
}
